import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Change from '../views/Change.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
    children: [
        { path: "/", redirect: "/users" },
        {
            path: "/users",
            component: () =>
                import ('../components/Users.vue')
        },
        {
            path: "/roles",
            component: () =>
                import ('../components/Juese.vue')
        },
        {
            path: "/rights",
            component: () =>
                import ('../components/Quanxian.vue')
        },
        {
            path: "/goods",
            component: () =>
                import ('../components/Goodslist.vue')
        },
        {
            path: "/params",
            component: () =>
                import ('../components/GoodsClassfiynum.vue')
        },
        {
            path: "/categories",
            component: () =>
                import ('../components/Goodslistclassfiy.vue')
        },
        {
            path: "/orders",
            component: () =>
                import ('../components/Dingdan.vue')
        },
        {
            path: "/reports",
            component: () =>
                import ('../components/Shuju.vue')
        },

    ]
}, {
    path: "/login",
    name: 'Login',
    component: () =>
        import ('../views/Login.vue')
}, {
    path: "/change",
    name: "Change",
    component: Change
}]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    //to:去哪个路径
    //from：从哪个路径来
    //next() 是否通过
    console.log(sessionStorage.getItem("token"))
    if (sessionStorage.getItem("token")) {
        next();
    } else {
        if (to.path != '/login') {
            next('/login')
        } else {
            next()
        }
    }
})
export default router